home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2223 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  67 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 17:17:36 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4dp5a0$bh9@umbc9.umbc.edu>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Joel Weinstein <surgsw@mizzou1.missouri.edu> wrote:
  13. |> I have been trying to get this very simple piece of code to work for
  14. |> hours.  What is the problem???????
  15. |> 
  16. |> #include <stdio.h>
  17. |> 
  18. |> main()
  19. |> {
  20. |>    int i=0;
  21. |>    char  word[100], c;
  22. |>    printf("Enter a word:   ");
  23. |>    while( (c = getchar()) != '\n')  {
  24.  
  25. What does getchar() return? An int nor a char (silly huh?)...You must make 'c'
  26. be an int or at the very least a signed char may work.
  27.  
  28. |>       *word = c;
  29.  
  30. This assigns 'c' to the first position of the word array so:
  31.  
  32. word[0] = c;
  33.  
  34. would be equivalent.
  35.  
  36. |>       word == word + 1;
  37.  
  38. You are comparing the array 'word' with 'word' + 1. Strange! I think you're
  39. trying to do pointer arithmetic, but alas, an array is not a pointer so
  40. don't do this.
  41.  
  42. |>    }  
  43. |>    printf("You entered:  ");  
  44. |>    *word = 0;
  45. |>    while( *word != '\n' )   
  46. |>    {  
  47. |>       putchar( *word );
  48. |>       word == word + 1; 
  49.  
  50. Again == is C's equality operator.
  51.  
  52. |>    }
  53. |> }  
  54. |> 
  55. |> I think the problem I am having is due to my not knowing when to use the
  56. |> '*' operator.  When do you use it?  Also, why won't my goddam compiler let
  57. |> me do word++; instead of word == word +1;   I also tried to do *word++;
  58. |> and it didn't work, what is the deal with that.  It won't let me put word
  59. |> = word + 1;.  It insists on the double ='s.  Why is that?
  60.  
  61. Well I guess not knowing the difference between arrays and pointers as
  62. well as which operators do what is your problem.
  63. -- 
  64. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  65.  
  66. Jonas J. Schlein  (schlein@gl.umbc.edu)
  67.